<template>
  <el-dialog
    :title="dialog.title"
    :visible.sync="dialogVisible"
    width="650px"
    @close="onClose"
  >
    <el-form
      ref="elForm"
      :model="formData"
      :rules="rules"
      size="medium"
      label-width="100px"
    >
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="formData.name"
          placeholder="请输入模板名称"
          :maxlength="30"
          show-word-limit
          clearable
          :style="{ width: '100%' }"
        ></el-input>
      </el-form-item>
      <el-form-item label="模板地址" prop="urls">
        <el-input
          v-model="formData.urls"
          placeholder="请输入模板地址"
          :maxlength="100"
          show-word-limit
          clearable
          :style="{ width: '100%' }"
        ></el-input>
      </el-form-item>
      <el-form-item label="可用状态" prop="status" required>
        <el-switch v-model="formData.status"></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import { getList, add, update } from '@/api/cms/template'
  import { changeTree } from '@/utils/treeTool'
  import { deepClone } from '@/utils/index'
  export default {
    data() {
      return {
        dialog: {
          title: '添加岗位',
        },
        formData: {
          name: undefined,
          urls: undefined,
          status: true,
        },
        rules: {
          name: [
            {
              required: true,
              message: '请输入模板名称',
              trigger: 'blur',
            },
          ],
          urls: [
            {
              required: true,
              message: '请输入模板地址',
              trigger: 'blur',
            },
          ],
        },
        dialogVisible: false,
        btnLoading: false,
      }
    },
    methods: {
      onClose() {
        this.$refs['elForm'].resetFields()
      },
      close() {
        this.dialogVisible = false
        this.$emit('update:visible', false)
      },
      handleAdd() {
        this.formData.id = '0'
        this.dialog.title = '添加模板'
        this.dialogVisible = true
      },
      handelModify(record) {
        this.dialog.title = '编辑模板'
        this.dialogVisible = true
        this.formData = deepClone(record)
      },
      handelConfirm() {
        this.$refs['elForm'].validate(async (valid) => {
          if (!valid) return
          let tipName = '添加成功'
          let res = null
          if (this.formData.id == '0') {
            this.formData.id = ''
            res = await add(this.formData)
          } else {
            tipName = '修改成功'
            res = await update(this.formData)
          }
          if (res.code == 200) {
            this.close()
            this.$emit('complete')
            this.$notify({
              message: tipName,
              type: 'success',
            })
          } else {
            this.$notify({
              message: message,
              type: 'error',
            })
          }
        })
      },
    },
  }
</script>
